package org.wisigoth.chat.client.jabber;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

import javax.swing.event.EventListenerList;

import org.wisigoth.chat.client.DefaultPresence;
import org.wisigoth.chat.client.Presence;
import org.wisigoth.chat.client.UserPreferencesEvent;
import org.wisigoth.chat.client.UserPreferencesListener;
import org.wisigoth.chat.client.utils.WisigothClientUtilities;

public class UserPreferencesImpl implements
		org.wisigoth.chat.client.UserPreferences {

	private static final String USER_PREF_FILE_NAME = "userPreferences.properties";

	private Properties userPreferences;
	private File userPropertiesFile;
	private EventListenerList listenerList = new EventListenerList();

	public UserPreferencesImpl() {
		String userFolder = WisigothClientUtilities.getHomeFolder()
				+ File.separator + "wisigoth";
		File userFolderFile = new File(userFolder);
		if (!userFolderFile.exists()) {
			userFolderFile.mkdir();
		}
		this.userPropertiesFile = new File(userFolder, USER_PREF_FILE_NAME);
		if (!this.userPropertiesFile.exists()) {
			try {
				this.userPropertiesFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			this.userPreferences = new Properties();
			Reader reader = new FileReader(this.userPropertiesFile);
			this.userPreferences.load(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void storeLoginParameters(String login, char[] pwd) {
		login = (login == null) ? "" : login;
		this.userPreferences.setProperty(LOGIN_KEY, login);
		String passwordString = (pwd == null) ? "" : new String(pwd);
		this.userPreferences.setProperty(PWD_KEY, passwordString);
		this.savePropertiesOnDisk();
		this
				.fireUserPreferencesEvent(new UserPreferencesEvent(this,
						LOGIN_KEY));
		this.fireUserPreferencesEvent(new UserPreferencesEvent(this, PWD_KEY));
	}

	public void storePresenceParameters(String presence) {
		presence = (presence == null) ? "Available" : new String(presence);
		this.userPreferences.setProperty(PRESENCE_KEY, presence);
		this.savePropertiesOnDisk();
	}

	private void savePropertiesOnDisk() {
		try {
			Writer writer = new FileWriter(this.userPropertiesFile);
			this.userPreferences.store(writer, null);
			writer.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	protected void fireUserPreferencesEvent(UserPreferencesEvent event) {
		for (UserPreferencesListener listener : this.listenerList
				.getListeners(UserPreferencesListener.class)) {
			listener.userPreferencesUpdated(event);
		}
	}

	@Override
	public void addUserPreferencesListener(UserPreferencesListener listener) {
		this.listenerList.add(UserPreferencesListener.class, listener);

	}

	@Override
	public void removeUserPreferencesListener(UserPreferencesListener listener) {
		this.listenerList.remove(UserPreferencesListener.class, listener);
	}

	@Override
	public String getLoginPreference() {
		return this.userPreferences.getProperty(LOGIN_KEY);
	}

	@Override
	public char[] getPasswordPreference() {
		// TODO Auto-generated method stub
		char[] result = null;
		String userPref = this.userPreferences.getProperty(PWD_KEY);

		if (userPref != null && userPref.trim().length() > 0) {
			result = this.userPreferences.getProperty(PWD_KEY).toCharArray();
			;
		}
		return result;
	}

	@Override
	public Presence getLastPresence() {
		Presence result = null;
		String presence = this.userPreferences.getProperty(PRESENCE_KEY);
		if (presence == null) {
			return DefaultPresence.AVAILABLE;
		}
		return DefaultPresence.valueOf(presence);
	}
}
